package com.online.teaching.module.admin.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.online.teaching.dao.StudentMapper;
import com.online.teaching.dao.TeacherMapper;
import com.online.teaching.dao.UserMapper;
import com.online.teaching.module.admin.user.domian.dto.StudentAddDTO;
import com.online.teaching.module.admin.user.domian.dto.TeacherAddDTO;
import com.online.teaching.module.admin.user.domian.vo.StudentGetVO;
import com.online.teaching.module.admin.user.domian.vo.TeacherGetVO;
import com.online.teaching.module.admin.user.service.AdminUserService;
import com.online.teaching.module.teacher.teaches.domain.vo.GetStudentListVO;
import com.online.teaching.pojo.Student;
import com.online.teaching.pojo.Teacher;
import com.online.teaching.utils.SmartBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import com.online.teaching.pojo.User;
import org.springframework.test.annotation.Rollback;

import java.util.List;

/**
 * @ClassName : AdminServiceImpl  //类名
 * @Description :   //描述
 * @Author : YG  //作者
 * @Date: 2020-11-04 13:28  //时间
 *
 * impl不给注释了，自己到service里面找去,由于权限校验security框架出了点问题,就先这样
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Rollback
    @Override
    public int addStudent(StudentAddDTO studentAddDTO,Integer userId){

        if(!check(userId)){
            return 0;
        }

        User user=new User();
        user.setUserAccount(studentAddDTO.getUserAccount());
        user.setPassword(new BCryptPasswordEncoder().encode(new StringBuilder(studentAddDTO.getUserAccount().substring(studentAddDTO.getUserAccount().length()-6)).toString()));
        Student copy = SmartBeanUtil.copy(studentAddDTO, Student.class);
        studentMapper.insertSelective(copy);
        user.setUserIdentiId(copy.getStuId());
        user.setUserType(1);
        int i = userMapper.insertSelective(user);
        return i;
    }

    @Rollback
    @Override
    public int addTeacher(TeacherAddDTO teacherAddDTO, Integer userId) {
        if(!check(userId)){
            return 0;
        }
        User user=new User();
        user.setUserAccount(teacherAddDTO.getUserAccount());
        user.setPassword(new BCryptPasswordEncoder().encode(new StringBuilder(teacherAddDTO.getUserAccount().substring(teacherAddDTO.getUserAccount().length()-6)).toString())) ;
        Teacher copy = SmartBeanUtil.copy(teacherAddDTO, Teacher.class);
        teacherMapper.insertSelective(copy);
        user.setUserIdentiId(copy.getTeaId());
        user.setUserType(2);
        int i=userMapper.insertSelective(user);
        return i;
    }

    @Override
    public int updatePassword(Integer userId,Integer userType, Integer adminId) {
        if(!check(adminId)){
            return 0;
        }
//        User user=new User();
//        User oldUser = userMapper.selectByPrimaryKey(userId);
//        if(oldUser==null){
//            return 0;
//        }
//        user.setPassword(new BCryptPasswordEncoder().encode(new StringBuilder(user.getUserAccount().substring(user.getUserAccount().length()-6)).toString())) ;
//        return userMapper.updateByPrimaryKeySelective(user);
        User user = userMapper.findUserByTypeAndIId(userType, userId);
        user.setPassword(new BCryptPasswordEncoder().encode(new StringBuilder(user.getUserAccount().substring(user.getUserAccount().length()-6)).toString())) ;
        return userMapper.updateByPrimaryKeySelective(user);

    }

    @Rollback
    @Override
    public int deleteUser(Integer userId, Integer userType, Integer adminId) {
        if(!check(adminId)){
            return 0;
        }
        User user = userMapper.findUserByTypeAndIId(userType, userId);
        if(user==null){
            return 0;
        }
        user.setIsValid(0);
        if(userType==1){
            Student student=new Student();
            student.setStuId(userId);
            student.setIsValid(0);
            studentMapper.updateByPrimaryKeySelective(student);
        }else {
            Teacher teacher=new Teacher();
            teacher.setTeaId(userId);
            teacher.setIsValid(0);
            teacherMapper.updateByPrimaryKeySelective(teacher);
        }
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public PageInfo<StudentGetVO> getTeacherList(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<Student> studentList = studentMapper.getStudentList();
        List<StudentGetVO> getStudentListVOS = SmartBeanUtil.copyList(studentList, StudentGetVO.class);
        return new PageInfo<>(getStudentListVOS);
    }

    @Override
    public PageInfo<TeacherGetVO> getStudentList(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<Teacher> teacherList = teacherMapper.getTeacherList();
        List<TeacherGetVO> teacherGetVOS = SmartBeanUtil.copyList(teacherList, TeacherGetVO.class);
        return new PageInfo<>(teacherGetVOS);
    }


    boolean check(Integer id){
        User user = userMapper.selectByPrimaryKey(id);
        if(user==null||user.getUserType()!=4){
            return false;
        }
        return true;
    }
}
